﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace TestHarness
{
    class Program
    {
        static void Main(string[] args)
        {
            //CreateSchema();

            Savage.SecureData.Factory sdf = new Savage.SecureData.Factory(ConnectionString);
            // Save
            Savage.SecureData.Securable securable = new Savage.SecureData.Securable("person", Savage.SecureData.DocumentType.Xml, "<Person><GivenNames>Alan</GivenNames><FamilyName>Savage</FamilyName><Gender>Male</Gender></Person>");
            sdf.Save(securable);
            Console.Out.WriteLine(securable.SecurableId);

            securable.Data = System.Xml.Linq.XDocument.Parse("<Person><GivenNames>Alan</GivenNames><FamilyName>Savage</FamilyName><Gender>alien</Gender></Person>");
            sdf.Save(securable);

            //edit it and save again
            
            // Find
            foreach (var s in sdf.FindXQuery(null, Savage.SecureData.DocumentType.Xml, null))
            {
                Console.Out.WriteLine(s.Data.ToString());
            }

            // Get
            securable = sdf.Get(new Guid("B41C7E4D-2A34-4D2B-A68E-CFAB2DD8BE2B"));
            Console.Out.WriteLine(securable.Data.ToString());
            
            Savage.SecureData.Factory factory = new Savage.SecureData.Factory(ConnectionString);
            //factory.CreateRole("test role");

            foreach (string s in factory.ListRoles())
            {
                Console.Out.WriteLine(s);
            }
        }

        static void CreateSchema()
        {
            string schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><xs:element name=\"GivenNames\" type=\"xs:string\"/><xs:element name=\"FamilyName\" type=\"xs:string\"/><xs:element name=\"Gender\" type=\"xs:string\"/></xs:schema>";
            Savage.SecureData.Securable s = new Savage.SecureData.Securable("person", Savage.SecureData.DocumentType.Xsd, schema);
            
            Savage.SecureData.Factory factory = new Savage.SecureData.Factory(ConnectionString);
            factory.Save(s);
        }
        static string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
        }
    }
}
